Input with datetime validation and transformation. In browsers that do not yet support the HTML5 date input, a text element will be used. In that case, the text must be entered in a valid ISO-8601 local datetime format (yyyy-MM-ddTHH:mm), for example: 2010-12-28T14:57
. The model must be a Date object.
<input type="dateTimeLocal"
ng-model=""
[name=""]
[min=""]
[max=""]
[required=""]
[ng-required=""]
[ng-change=""]>
Param | Type | Details |
---|---|---|
ngModel | string | Assignable angular expression to data-bind to. |
name
(optional) | string | Property name of the form under which the control is published. |
min
(optional) | string | Sets the |
max
(optional) | string | Sets the |
required
(optional) | string | Sets |
ngRequired
(optional) | string | Adds |
ngChange
(optional) | string | Angular expression to be executed when input changes due to user interaction with the input element. |
<script>
angular.module('dateExample', [])
.controller('DateController', ['$scope', function($scope) {
$scope.value = new Date(2010, 11, 28, 14, 57);
}]);</script><form name="myForm" ng-controller="DateController as dateCtrl">
Pick a date between in 2013:
<input type="datetime-local" id="exampleInput" name="input" ng-model="value"
placeholder="yyyy-MM-ddTHH:mm" min="2001-01-01T00:00" max="2013-12-31T00:00" required />
<span class="error" ng-show="myForm.input.$error.required">
Required!</span>
<span class="error" ng-show="myForm.input.$error.datetimelocal">
Not a valid date!</span>
<tt>value = {{value | date: "yyyy-MM-ddTHH:mm"}}</tt><br/>
<tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
<tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
<tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/></form>
protractor.jsvar value = element(by.binding('value | date: "yyyy-MM-ddTHH:mm"'));var valid = element(by.binding('myForm.input.$valid'));var input = element(by.model('value'));
// currently protractor/webdriver does not support// sending keys to all known HTML5 input controls// for various browsers (https://github.com/angular/protractor/issues/562).function setInput(val) {
// set the value of the element and force validation.
var scr = "var ipt = document.getElementById('exampleInput'); " +
"ipt.value = '" + val + "';" +
"angular.element(ipt).scope().$apply(function(s) { s.myForm[ipt.name].$setViewValue('" + val + "'); });";
browser.executeScript(scr);}
it('should initialize to model', function() {
expect(value.getText()).toContain('2010-12-28T14:57');
expect(valid.getText()).toContain('myForm.input.$valid = true');});
it('should be invalid if empty', function() {
setInput('');
expect(value.getText()).toEqual('value =');
expect(valid.getText()).toContain('myForm.input.$valid = false');});
it('should be invalid if over max', function() {
setInput('2015-01-01T23:59');
expect(value.getText()).toContain('');
expect(valid.getText()).toContain('myForm.input.$valid = false');});